package cn.itcast.service.cargo.impl;

import cn.itcast.dao.cargo.ContractDao;
import cn.itcast.dao.cargo.ExtCproductDao;
import cn.itcast.domain.cargo.Contract;
import cn.itcast.domain.cargo.ExtCproduct;
import cn.itcast.domain.cargo.ExtCproductExample;
import cn.itcast.service.cargo.ExtCProductService;
import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;
import java.util.UUID;

@Service
public class ExtCProductServiceImpl implements ExtCProductService {

	@Autowired
	private ExtCproductDao extCproductDao; //附件dao

	@Autowired
	private ContractDao contractDao; //合同dao

	/**
	 * 保存附件
	 *  参数：附件对象
	 *       包含了货物id：contractProductId
	 *       包含了合同id：contractId
	 */
	public void save(ExtCproduct extc) {
		//1.计算保存的附件金额
		double money = 0.0;
		if(extc.getCnumber() != null && extc.getPrice() != null) {
			money = extc.getCnumber() * extc.getPrice();
		}
		extc.setAmount(money);
		//2.设置附件的ID
		extc.setId(UUID.randomUUID().toString());
		//3.保存附件
		extCproductDao.insertSelective(extc);
		//4.根据合同id查询合同
		Contract contract = contractDao.selectByPrimaryKey(extc.getContractId());
		//5.设置合同的附件数和总金额
		contract.setTotalAmount(contract.getTotalAmount() + money);
		contract.setExtNum(contract.getExtNum() + 1);
		//6.更新合同
		contractDao.updateByPrimaryKeySelective(contract);

	}

	/**
	 * 更新附件
	 *    参数：附件对象
	 *       包含了货物id：contractProductId
	 *       包含了合同id：contractId
	 */
	public void update(ExtCproduct extc) {
		//1.计算更新之后的金额
		double newMoney = 0.0;
		if(extc.getCnumber() != null && extc.getPrice() != null) {
			newMoney = extc.getCnumber() * extc.getPrice();
		}
		extc.setAmount(newMoney);
		//2.查询更新之前的金额
		ExtCproduct oldExtc = extCproductDao.selectByPrimaryKey(extc.getId());
		double oldMoney = oldExtc.getAmount();
		//3.更新附件
		extCproductDao.updateByPrimaryKeySelective(extc);
		//4.根据合同id查询合同
		Contract contract = contractDao.selectByPrimaryKey(extc.getContractId());
		//5.设置合同的总金额
		contract.setTotalAmount(contract.getTotalAmount() - oldMoney + newMoney);
		//6.更新合同
		contractDao.updateByPrimaryKeySelective(contract);
	}

	/**
	 * 根据id删除附件
	 *  参数：附件id
	 */
	public void delete(String id) {
		//1.根据id查询附件（合同id和金额）
		ExtCproduct extc = extCproductDao.selectByPrimaryKey(id);
		//2.删除附件
		extCproductDao.deleteByPrimaryKey(id);
		//3.根据合同id查询合同
		Contract contract = contractDao.selectByPrimaryKey(extc.getContractId());
		//4.设置合同的总金额和附件数量
		contract.setTotalAmount(contract.getTotalAmount() - extc.getAmount());
		contract.setExtNum(contract.getExtNum() - 1);
		//5.更新合同
		contractDao.updateByPrimaryKeySelective(contract);
	}

	@Override
	public ExtCproduct findById(String id) {
		return extCproductDao.selectByPrimaryKey(id);
	}

	@Override
	public PageInfo findAll(int page, int size, ExtCproductExample example) {
		PageHelper.startPage(page,size);
		List<ExtCproduct> list = extCproductDao.selectByExample(example);
		return new PageInfo(list);
	}
}
